package com.psychproject.web.controllers;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.psychproject.web.domains.HelperGroup;
import com.psychproject.web.services.HelperGroupServices;
import com.psychproject.web.services.UserServices;

@Controller
@RequestMapping(value = "/helpergroup")
public class HelperGroupController {
	@Autowired
	private HelperGroupServices helperGroupServices;
	@Autowired
	private UserServices userServices;
	@RequestMapping(method = RequestMethod.GET)
	public String getCreateForm(Model model) {
		model.addAttribute("helperGroup",new HelperGroup());
		model.addAttribute("users",userServices.all());
		return "helpergroup/create";
	}

	@RequestMapping(method = RequestMethod.POST)
	public String create(HelperGroup helperGroup, BindingResult result) {
		helperGroupServices.createHelperGroup(helperGroup);
		return "redirect:/helpergroup/" + helperGroup.getId();
	}
	
	@RequestMapping(value="{id}", method=RequestMethod.GET)
	public String getView(@PathVariable Long id, Model model) {
		HelperGroup helperGroup = helperGroupServices.findById(id);
		model.addAttribute(helperGroup);
		return "helpergroup/view";
	}
}
